﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmashedApples.Utils
{
    public static class DataUtils
    {

        public static String CreateLikeValue(string param)
        {
            return "%" + param.ToUpper().Trim().Replace(" ", "%") + "%";
        }

        public static DateTime LastDayOfWeek(DateTime date)
        {
            DateTime firstDayOfWeek = FirstDayOfWeek(date);
            return firstDayOfWeek.AddDays(6);
        }

        public static DateTime FirstDayOfWeek(DateTime date)
        {

            double offset = 0;
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    offset = 0;
                    break;
                case DayOfWeek.Monday:
                    offset = -1;
                    break;
                case DayOfWeek.Tuesday:
                    offset = -2;
                    break;
                case DayOfWeek.Wednesday:
                    offset = -3;
                    break;
                case DayOfWeek.Thursday:
                    offset = -4;
                    break;
                case DayOfWeek.Friday:
                    offset = -5;
                    break;
                case DayOfWeek.Saturday:
                    offset = -6;
                    break;
            }

            return date.AddDays(offset);
        }


        //public static Dictionary<String, ColumnMetaData> GetColumnMetaData(String tableName)
        //{

        //    //Dictionary<String, ColumnMetaData> tableDef = (Dictionary<String, ColumnMetaData>)HttpContext.Current.Application["wgc::tables::" + tableName];

        //    Dictionary<String, ColumnMetaData> tableDef = null;

        //    if (tableDef == null)
        //    {
        //        tableDef = new Dictionary<string, ColumnMetaData>();
        //        DataArgs args = new DataArgs("ALL_TAB_COLUMNS");
        //        args.Parameters.Add("TABLE_NAME", tableName);
        //        args.Columns = "OWNER,TABLE_NAME,COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE";
        //        args.ReturnRowID = false;

        //        DataTable dt = DataService.Select(args);

        //        foreach (DataRow row in dt.Rows)
        //        {
        //            ColumnMetaData columnMetaData = new ColumnMetaData(row);
        //            tableDef.Add((String)columnMetaData["COLUMN_NAME"], columnMetaData);
        //        }

        //        // lock and write to application scope
        //        //HttpContext.Current.Application.Lock();
        //        ///HttpContext.Current.Application["wgc::tables::" + tableName] = tableDef;
        //        //HttpContext.Current.Application.UnLock();
        //    }


        //    return tableDef;
        //}



        public static String DecodeOneZeroToYN(int value)
        {
            if (value == 0)
                return "N";
            return "Y";
        }
    }
}
